Page({
  data: {
    accountInfo: {
      id: 1,
      companyName: '公司名称有很多信息',
      userName: '张晓',
      address: '上海市崇明区长兴镇江南大道1899898号楼三单元1188室',
      createTime: '2020-01-01 12:00:00',
      status: 1,
      typeIndex: '1',
      isAutoSeparate: false,
      accountList: [{
        id: 1,
        userName: '张晓',
        ratio: 12.1,
        amount: 1000,
      }, {
        id: 2,
        userName: '孔东方',
        ratio: 1.1,
        amount: 960.01,
      }, {
        id: 3,
        userName: '车砌墙',
        ratio: 5.1,
        amount: 1800.01,
      }]
    },
    typeOptions: {
      '1': '比例分账',
      '2': '定额分账',
      '3': '自动分账',
    },
    typeIndex: '1',
    isAutoSeparate: false,
    accountList: [],
    errorIndex:-1
  },
  onLoad(options) {
    this.initData()
  },
  initData(index) {
    const { accountInfo } = this.data
    this.setData({
      typeIndex: index ? index : (accountInfo?.typeIndex || '1'),
      isAutoSeparate: accountInfo?.isAutoSeparate || false,
      accountList: accountInfo?.accountList || []
    })
  },
  handlerNavChange(e) {
    const { index } = e.currentTarget.dataset
    this.initData(index)
  },
  handlerChangeAutoSeparate(e) {
    const { value } = e.detail
    this.setData({
      isAutoSeparate: value
    }, () => {
      this.handlerSubmit()
    })
  },
  handlerInput(e) {
    const { filed, index } = e.currentTarget.dataset
    const { value } = e.detail
    const { accountList } = this.data
    accountList[index][filed] = value
    this.setData({
      accountList,
      errorIndex:-1
    })
  },
  handlerSubmit() {
    const { typeIndex, isAutoSeparate, accountList, accountInfo } = this.data
    let params = {}
    if (typeIndex === '3') {
      params = {
        typeIndex,
        isAutoSeparate,
        id: accountInfo.id,
      }
    } else {
      for (let i = 0; i < accountList.length; i++) {
        const { amount, ratio } = accountList[i]
        const value = Number(typeIndex === '1' ? ratio : amount)
        if (isNaN(value)) {
          wx.showToast({
            title: '请输入正确数字',
            icon: 'none'
          })
          this.setData({
            errorIndex: i
          })
          return
        }
      }
      params = {
        typeIndex,
        accountList,
        id: accountInfo.id,
      }
    }
    console.log('[ params ]-76', params)
  },
  onPullDownRefresh() {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 2000)
  },
  onShareAppMessage() {

  }
})